package com.jdz.controller.portal;

import com.jdz.common.Const;
import com.jdz.formBean.UserLoginForm;
import com.jdz.pojo.User;
import com.jdz.common.ResponseCode;
import com.jdz.common.ServerResponse;
import com.jdz.service.EmailService;
import com.jdz.service.IUserService;
import com.jdz.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by sun
 **/
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private EmailService emailService;

    /**
     * 用户登录
     * @param userLoginForm
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    public ServerResponse<UserVo> login(UserLoginForm userLoginForm , HttpSession session){
        //service-->mybatis-->dao
        ServerResponse<UserVo> response = iUserService.login(userLoginForm.getUsername(),userLoginForm.getUserpwd());
        if (response.isSuccess()){
            session.setMaxInactiveInterval(1800);//秒为单位，1800= 60*30 即30分种
            session.setAttribute(Const.CURRENT_USER,response.getData());
            System.out.println(session.getId());
        }
        return response;
    }

    /**
     * 用户退出登录
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "logout.do",method = RequestMethod.GET)
    public ServerResponse<String> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        if(session.getAttribute(Const.CURRENT_USER)==null){
            return ServerResponse.createBySuccessMessage("注销成功");
        }
        return ServerResponse.createByErrorMessage("注销失败");
    }


//    前台birthday对应user.birthday,当然user的这个属性是Date类型,前台传过来的是字符类型,
//    一般情况下spring就会自动进行前后台的自动匹配,但是数据类型不一致会报错,说前台传过来的数据不合法.
//            只需在Controller中定义一个方法:
//    @InitBinder
//    public void initBinder(ServletRequestDataBinder binder){
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
//    }
//    作用即是将前台传入的String类型birthday经SimpleDateFormat转换成Date类型birthday从而和user.birthday匹配.

    /**
     * 用户注册
     * @param user
     * @return
     */
    @RequestMapping(value = "register.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> register( User user){
        return iUserService.register(user);
    }

    /**
     * 校验email和username是否存在
     * 为了点击下个输入框的时候时刻校验
     * @param str
     * @param type
     * @return
     */
    @RequestMapping(value = "check_valid.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> checkValid(String str,String type){
        return iUserService.checkValid(str,type);
    }

    /**
     * 获取登陆用户session的信息
     * @param session
     * @return
     */
    @RequestMapping(value = "get_user_info.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<UserVo> getUserInfo(HttpSession session){
        UserVo userVo = (UserVo) session.getAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess(userVo);
    }

//    /**
//     * 密码提示问题的获取
//     * @param username
//     * @return
//     */
//    @RequestMapping(value = "forget_get_question.do",method = RequestMethod.POST)
//    @ResponseBody
//    public ServerResponse<String> forgetGetQuestion(String username){
//        return iUserService.selectQuestion(username);
//    }
//
//    /**
//     * 检查答案
//     * 使用本地缓存返回tonken
//     * @param username
//     * @param question
//     * @param answer
//     * @return
//     */
//    @RequestMapping(value = "forget_check_answer.do",method = RequestMethod.POST)
//    @ResponseBody
//    public ServerResponse<String> forgetCheckAnswer(String username,String question,String answer){
//        return iUserService.checkAnswer(username,question,answer);
//    }
//
//    /**
//     * 必须传入token
//     * 忘记密码中的修改密码
//     * @param username
//     * @param passwordNew
//     * @param forgetToken
//     * @return
//     */
//    @RequestMapping(value = "forget_reset_password.do",method = RequestMethod.POST)
//    @ResponseBody
//    public ServerResponse<String> forgetRestPassword(String username,String passwordNew,String forgetToken){
//        return iUserService.forgetResetPassword(username,passwordNew,forgetToken);
//    }

    /**
     * 登陆状态下重设密码
     * @param session
     * @param passwordOld
     * @param passwordNew1
     * @param passwordNew2
     * @return
     */
    @RequestMapping(value = "reset_password.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> resetPassword(HttpSession session,String passwordOld,String passwordNew1,String passwordNew2){
        UserVo userVo= (UserVo) session.getAttribute(Const.CURRENT_USER);
        return iUserService.resetPassword(passwordOld,passwordNew1,passwordNew2,userVo);
    }

    /**
     * 更新用户个人信息
     * @param session
     * @param user
     * @return
     */
    @RequestMapping(value = "update_information.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<UserVo> update_information(HttpSession session,User user){
        UserVo currentUser = (UserVo) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMessage("用户未输入修改内容");
        }
        //为了防止越权，第一先把传过来的user id设置成从session中获取的Id
        user.setStuno(currentUser.getStuno());
        //学号也需要从session获取下，传过去要更新的
        //一会返回值一个User也需要username,要不然session存的user就没username了所以一定要有username的
        user.setStuid(currentUser.getStuid());
        ServerResponse<UserVo> response = iUserService.updateInformation(user);
        if(response.isSuccess()){
            response.getData().setStuid(currentUser.getStuid());
            response.getData().setStupassword(StringUtils.EMPTY);
            response.getData().setLockDate(currentUser.getLockDate());
            response.getData().setLockTimes(currentUser.getLockTimes());
            response.getData().setStatus(currentUser.getStatus());
            response.getData().setGroupno(currentUser.getGroupno());
            response.getData().setGroupname(currentUser.getGroupname());
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }


    /**
     *获取用户个人信息
     * @param session
     * @return
     */
    @RequestMapping(value = "get_information.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<UserVo> get_information(HttpSession session){
        UserVo currentUser = (UserVo) session.getAttribute(Const.CURRENT_USER);
        return iUserService.getInformation(currentUser.getStuno());
    }

//    /**
//     * 获得信息验证码
//     * @param request
//     * @param username
//     * @return
//     */
//    @RequestMapping(value = "get_password.do",method = RequestMethod.POST)
//    @ResponseBody
//    public ServerResponse getpassword(HttpServletRequest request, String username){
//        return iUserService.getPassword(request,username);
//    }

    /**
     * 获得邮箱验证码
     * @param request
     * @param username
     * @return
     */
    @RequestMapping(value = "get_email.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getpassword(HttpSession session,HttpServletRequest request, String username){
        return emailService.sendMailSimple(session,username);
    }

    /**
     * 通过验证码修改密码
     * @param session
     * @param yanzhenFrom
     * @param password
     * @return
     */
    @RequestMapping(value = "set_email_password.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getpassword(HttpSession session,String yanzhenFrom,String password){
        return emailService.setEmaill(session,yanzhenFrom,password);
    }
}
